package org.dandelero.properties.framework;

/**
 * A predicate that applies two predicates and returns true both evaluate successfully.
 * 
 * @author dandelero
 * 
 * @param <T>
 *            the type of data to be checked.
 */
public class AndPredicate<T> implements Predicate<T> {

	// ==================================================
	// Fields.
	// ==================================================

	/**
	 * The two predicates to apply.
	 */
	private final Predicate<T> predicate1, predicate2;

	// ==================================================
	// Initialisation.
	// ==================================================

	/**
	 * Constructs a predicate that returns true when both predicates return true.
	 * 
	 * @param predicate1
	 *            the first predicate.
	 * @param predicate2
	 *            the second predicate.
	 */
	public AndPredicate(Predicate<T> predicate1, Predicate<T> predicate2) {
		this.predicate1 = predicate1;
		this.predicate2 = predicate2;
	}

	// ==================================================
	// Predicate implementation.
	// ==================================================

	/**
	 * The check as to whether a property should be accepted or rejected.
	 * 
	 * @param o
	 *            the object.
	 * @return true or false.
	 */
	public boolean evaluate(T o) {
		return this.predicate1.evaluate(o) && this.predicate2.evaluate(o);
	}
}
